<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import * as echarts from 'echarts'

// 顶部数据概览
const stats = [
  { icon: 'el-icon-s-order', label: '付款订单', value: 231, color: '#409EFF' },
  { icon: 'el-icon-money', label: '付款金额(元)', value: 1000.21, color: '#F56C6C' },
  { icon: 'el-icon-user', label: '活跃用户(今日)', value: 120311, color: '#67C23A' },
  { icon: 'el-icon-pie-chart', label: '转化率(%)', value: 0.53, color: '#E6A23C' },
  { icon: 'el-icon-coin', label: '客单价(元)', value: 32.00, color: '#909399' },
  { icon: 'el-icon-user-solid', label: '新增用户(今日)', value: 122, color: '#36CFC9' },
]

// 快捷入口
const shortcuts = [
  { icon: 'el-icon-plus',  label: '新增商品' },
  { icon: 'el-icon-goods', label: '商品管理' },
  { icon: 'el-icon-truck', label: '发货管理' },
  { icon: 'el-icon-user', label: '团长申请' },
  { icon: 'el-icon-s-order', label: '订单管理' },
  { icon: 'el-icon-tickets', label: '佣金明细' },
]

// 待办事项
const todos = [
  { label: '待付款订单', count: 11 },
  { label: '待发货社区', count: 11 },
  { label: '团长申请审核', count: 1 },
  { label: '售后订单', count: 1 },
  { label: '商品库存不足', count: 1 },
  { label: '广告即将到期', count: 1 },
]

// 商品销售排行榜
const productRank = [
  { name: '伊利纯牛奶 1020g 51个装', value: 320102 },
  { name: '俏依 华油菜籽油1020g 51个装', value: 223234 },
  { name: '汰渍 Tide 专业级除菌净护洗衣液600g/瓶', value: 123234 },
  { name: 'LOVO乐蜗家纺60支贡缎全棉四件套-上四季', value: 98322 },
  { name: 'AOC AGON 爱攻 27寸', value: 83522 },
  { name: '海天酱油 鲜味生抽1.28L', value: 64522 },
  { name: '光明中杯250ml*24箱', value: 45522 },
]

// 团长销售排行榜
const leaderRank = [
  { name: 'mounika', area: '石景山社区', value: 323234 },
  { name: '王多多', area: '大树林社区', value: 301223 },
  { name: '花翠婷', area: '团购社区', value: 287322 },
  { name: '小乐乐', area: '团购社区', value: 250094 },
  { name: '安心', area: '团购社区', value: 200321 },
  { name: '小甜甜', area: '团购社区', value: 173981 },
  { name: 'sdgeee', area: '团购社区', value: 150092 },
]

// 订单统计、交易统计图表数据
const orderChart = ref()
const tradeChart = ref()

const chartTabs = ref('month')

const orderChartData = {
  x: ['02/01', '02/02', '02/03', '02/04', '02/05', '02/06'],
  y: [3000, 4000, 3500, 5000, 4800, 4200],
}
const tradeChartData = {
  x: ['02/01', '02/02', '02/03', '02/04', '02/05', '02/06'],
  y: [3200, 4100, 3700, 5100, 4900, 4300],
}

const initCharts = () => {
  // 订单统计
  orderChart.value = echarts.init(document.getElementById('orderChart'))
  orderChart.value.setOption({
    tooltip: { trigger: 'axis' },
    grid: { left: 40, right: 20, top: 40, bottom: 30 },
    xAxis: { type: 'category', data: orderChartData.x },
    yAxis: { type: 'value' },
    series: [{
      data: orderChartData.y,
      type: 'line',
      smooth: true,
      areaStyle: { opacity: 0.15 },
      lineStyle: { color: '#409EFF', width: 2 },
      itemStyle: { color: '#409EFF' },
    }],
  })
  // 交易统计
  tradeChart.value = echarts.init(document.getElementById('tradeChart'))
  tradeChart.value.setOption({
    tooltip: { trigger: 'axis' },
    grid: { left: 40, right: 20, top: 40, bottom: 30 },
    xAxis: { type: 'category', data: tradeChartData.x },
    yAxis: { type: 'value' },
    series: [{
      data: tradeChartData.y,
      type: 'line',
      smooth: true,
      areaStyle: { opacity: 0.15 },
      lineStyle: { color: '#67C23A', width: 2 },
      itemStyle: { color: '#67C23A' },
    }],
  })
}

const router = useRouter();
const shortcutRoutes = [
  '/product/management',      // 新增商品
  '/product/management',      // 商品管理
  '/order/shipping',          // 发货管理
  '/dashboard/leader-analysis', // 团长申请
  '/order/management',        // 订单管理
  '/finance/commission',      // 佣金明细
];
function handleShortcutClick(i) {
  router.push(shortcutRoutes[i]);
}

const todoRoutes = [
  '/order/management',           // 代付款订单
  '/order/shipping',            // 待发货社区
  '/dashboard/leader-analysis', // 团长申请审核
  '/order/after-sales',         // 售后订单
  '/product/management',        // 商品库存不足
  '/dashboard/operation',       // 广告即将到期（假设为广告管理页面）
];
function handleTodoClick(i) {
  router.push(todoRoutes[i]);
}

onMounted(() => {
  setTimeout(initCharts, 100)
})
</script>

<template>
  <div class="operation-dashboard">
    <!-- 顶部数据概览和快捷入口 -->
    <div class="top-section">
      <div class="stats-panel">
        <div class="stats-row">
          <div v-for="(item, i) in stats" :key="i" class="stat-card">
            <i :class="item.icon" :style="{color: item.color}"></i>
            <div class="stat-value">{{ item.value }}</div>
            <div class="stat-label">{{ item.label }}</div>
          </div>
        </div>
        <div class="todo-row">
          <button v-for="(item, i) in todos" :key="i" class="todo-btn" @click="handleTodoClick(i)">
            {{ item.label }} <span class="todo-count">({{ item.count }})</span>
          </button>
        </div>
      </div>
      <div class="shortcut-panel">
        <div class="shortcut-grid">
          <div v-for="(item, i) in shortcuts" :key="i" class="shortcut-btn" @click="handleShortcutClick(i)">
            <i :class="item.icon"></i>
            <span>{{ item.label }}</span>
          </div>
        </div>
      </div>
    </div>

    <!-- 下方主内容区 -->
    <div class="main-section">
      <div class="main-left">
        <!-- 订单统计 -->
        <div class="chart-card">
          <div class="chart-header">
            <span>订单统计</span>
            <div class="chart-tabs">
              <button :class="{active: chartTabs==='month'}" @click="chartTabs='month'">本月</button>
              <button :class="{active: chartTabs==='year'}" @click="chartTabs='year'">全年</button>
            </div>
            <div class="chart-info">订单总数：<b>10000</b> <span class="red">D10%</span></div>
          </div>
          <div id="orderChart" class="chart"></div>
        </div>
        <!-- 交易统计 -->
        <div class="chart-card">
          <div class="chart-header">
            <span>交易统计</span>
            <div class="chart-tabs">
              <button :class="{active: chartTabs==='month'}" @click="chartTabs='month'">本月</button>
              <button :class="{active: chartTabs==='year'}" @click="chartTabs='year'">全年</button>
            </div>
            <div class="chart-info">销售金额：<b>10000元</b> <span class="red">D10%</span></div>
          </div>
          <div id="tradeChart" class="chart"></div>
        </div>
      </div>
      <div class="main-right">
        <!-- 商品销售排行榜 -->
        <div class="rank-card">
          <div class="rank-header">
            <span>商品销售排行榜</span>
            <div class="rank-tabs">
              <button class="active">本月</button>
              <button>本周</button>
              <button>今日</button>
            </div>
          </div>
          <ul class="rank-list">
            <li v-for="(item, i) in productRank" :key="i">
              <span class="rank-num" :class="{top3: i<3}">{{ i+1 }}</span>
              <span class="rank-name">{{ item.name }}</span>
              <span class="rank-value">{{ item.value.toLocaleString() }}</span>
            </li>
          </ul>
        </div>
        <!-- 团长销售排行榜 -->
        <div class="rank-card">
          <div class="rank-header">
            <span>团长销售排行榜</span>
            <div class="rank-tabs">
              <button class="active">本月</button>
              <button>本周</button>
              <button>今日</button>
            </div>
          </div>
          <ul class="rank-list">
            <li v-for="(item, i) in leaderRank" :key="i">
              <span class="rank-num" :class="{top3: i<3}">{{ i+1 }}</span>
              <span class="rank-name">{{ item.name }}</span>
              <span class="rank-area">{{ item.area }}</span>
              <span class="rank-value">{{ item.value.toLocaleString() }}</span>
            </li>
          </ul>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.operation-dashboard {
  padding: 24px;
  background: #f5f7fa;
  min-height: 100vh;
  font-family: 'PingFang SC', 'Microsoft YaHei', Arial, sans-serif;
}
.top-section {
  display: flex;
  gap: 24px;
  margin-bottom: 24px;
}
.stats-panel {
  flex: 2;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0,0,0,0.06);
  padding: 24px 24px 12px 24px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}
.stats-row {
  display: flex;
  gap: 24px;
  margin-bottom: 16px;
}
.stat-card {
  flex: 1;
  background: #f7faff;
  border-radius: 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 18px 0 12px 0;
  box-shadow: 0 1px 4px rgba(64,158,255,0.04);
  transition: box-shadow 0.2s;
}
.stat-card:hover {
  box-shadow: 0 4px 16px rgba(64,158,255,0.10);
}
.stat-card i {
  font-size: 32px;
  margin-bottom: 8px;
}
.stat-value {
  font-size: 24px;
  font-weight: bold;
  color: #222;
  margin-bottom: 4px;
}
.stat-label {
  font-size: 14px;
  color: #888;
}
.todo-row {
  display: flex;
  gap: 18px;
  margin-top: 8px;
  padding-top: 12px;
  border-top: 1px solid #f0f0f0;
}
.todo-btn {
  font-size: 15px;
  color: #666;
  background: #f7faff;
  border: 1px solid #e6eaf0;
  border-radius: 6px;
  padding: 6px 18px;
  cursor: pointer;
  transition: background 0.2s, color 0.2s, border 0.2s;
  font-weight: 500;
  outline: none;
  display: flex;
  align-items: center;
  gap: 2px;
}
.todo-btn:hover {
  background: #409EFF;
  color: #fff;
  border-color: #409EFF;
}
.todo-count {
  color: #F56C6C;
  font-weight: bold;
  margin-left: 2px;
}
.shortcut-panel {
  flex: 1;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0,0,0,0.06);
  padding: 24px 0;
  display: flex;
  align-items: center;
  justify-content: center;
}
.shortcut-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(2, 1fr);
  gap: 18px;
  width: 100%;
}
.shortcut-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: #f7faff;
  border-radius: 8px;
  padding: 16px 0;
  font-size: 14px;
  color: #409EFF;
  cursor: pointer;
  transition: background 0.2s;
}
.shortcut-btn:hover {
  background: #e6f7ff;
}
.shortcut-btn i {
  font-size: 24px;
  margin-bottom: 6px;
}

.main-section {
  display: flex;
  gap: 24px;
}
.main-left {
  flex: 2;
  display: flex;
  flex-direction: column;
  gap: 24px;
}
.chart-card {
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0,0,0,0.06);
  padding: 20px 24px 8px 24px;
  margin-bottom: 0;
}
.chart-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}
.chart-tabs {
  display: flex;
  gap: 8px;
}
.chart-tabs button {
  background: #f7faff;
  border: none;
  border-radius: 6px;
  padding: 4px 14px;
  font-size: 13px;
  color: #409EFF;
  cursor: pointer;
  transition: background 0.2s;
}
.chart-tabs button.active, .chart-tabs button:hover {
  background: #409EFF;
  color: #fff;
}
.chart-info {
  font-size: 13px;
  color: #888;
}
.chart-info b {
  color: #222;
  font-weight: 600;
}
.chart-info .red {
  color: #F56C6C;
  margin-left: 4px;
}
.chart {
  width: 100%;
  height: 220px;
}

.main-right {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 24px;
}
.rank-card {
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0,0,0,0.06);
  padding: 20px 18px 12px 18px;
}
.rank-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}
.rank-tabs {
  display: flex;
  gap: 8px;
}
.rank-tabs button {
  background: #f7faff;
  border: none;
  border-radius: 6px;
  padding: 4px 10px;
  font-size: 13px;
  color: #409EFF;
  cursor: pointer;
  transition: background 0.2s;
}
.rank-tabs button.active, .rank-tabs button:hover {
  background: #409EFF;
  color: #fff;
}
.rank-list {
  list-style: none;
  padding: 0;
  margin: 0;
}
.rank-list li {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 0;
  border-bottom: 1px solid #f5f7fa;
  font-size: 14px;
}
.rank-list li:last-child {
  border-bottom: none;
}
.rank-num {
  width: 22px;
  height: 22px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  background: #f7faff;
  color: #409EFF;
  font-weight: 600;
}
.rank-num.top3 {
  background: #409EFF;
  color: #fff;
}
.rank-name {
  flex: 1;
  color: #222;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.rank-area {
  color: #888;
  font-size: 13px;
  margin-right: 8px;
}
.rank-value {
  color: #F56C6C;
  font-weight: 600;
  min-width: 70px;
  text-align: right;
}
@media (max-width: 1100px) {
  .top-section, .main-section {
    flex-direction: column;
  }
  .shortcut-panel, .stats-panel, .main-left, .main-right {
    width: 100%;
  }
}
</style>